package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.adapter.RoleAdapter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.presenter.contract.RoleChoiceContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.presenter.RoleChoicePresenter;
import com.shd.edanpei.util.AcCollector;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/10/19.
 * 身份选择
 */

public class RoleChoiceActivity extends BaseActivity<RoleChoicePresenter>
        implements RoleChoiceContract.RoleChoiceIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_submit)
    View submit;

    private RoleAdapter mAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_role_choice;
    }

    @Override
    protected RoleChoicePresenter bindPresenter() {
        return new RoleChoicePresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("身份选择");

        mAdapter = new RoleAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);

        mPresenter.initRoleData();
    }


    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    if (position == i) {
                        mPresenter.setData(mAdapter.getItem(i));
                        mAdapter.getItem(i).setCheck(true);
                        submit.setBackground(getResources().getDrawable(R.drawable.shape_main_button));
                    } else {
                        mAdapter.getItem(i).setCheck(false);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        DaoManager.getInstance().logout();
        AcCollector.finishOther(this);
        startActivity(LoginActivity.class);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBack();
        }
        return false;
    }

    @OnClick(R.id.tv_submit)
    void onSubmit() {
        mPresenter.submit();
    }


    @Override
    public RoleAdapter getAdapter() {
        return mAdapter;
    }
}
